{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4339b840",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0., 0., 0., 0., 0.],\n",
       "       [0., 1., 0., 0., 0., 0.],\n",
       "       [0., 0., 1., 0., 0., 0.],\n",
       "       [0., 0., 0., 1., 0., 0.],\n",
       "       [0., 0., 0., 0., 1., 0.],\n",
       "       [0., 0., 0., 0., 0., 1.]])"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "np.eye(6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "14b36e8f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "MyClass, i is :  123\n",
      "MyClass, f is :  hi yumoz\n",
      "3.0 -4.5\n",
      "<__main__.Complex object at 0x108a260d0>\n",
      "<class '__main__.Complex'>\n"
     ]
    }
   ],
   "source": [
    "class MyClass:\n",
    "    i = 123    \n",
    "    def func(self):\n",
    "        return 'hi yumoz'\n",
    "    \n",
    "class Complex:\n",
    "#     r = 0;\n",
    "#     i = 0;\n",
    "    def prt(self):\n",
    "        print(self)\n",
    "        print(self.__class__)\n",
    "    def __init__(self, realpart, imagpart):\n",
    "        self.r = realpart\n",
    "        self.i = imagpart\n",
    "# 实例化\n",
    "x = MyClass()\n",
    "\n",
    "print(\"MyClass, i is : \", x.i)\n",
    "print(\"MyClass, f is : \", x.func())\n",
    "\n",
    "y = Complex(3.0, -4.5)\n",
    "print(y.r, y.i)\n",
    "y.prt()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8ec2888e",
   "metadata": {},
   "source": [
    "分析：从执行结果可以很明显的看出，self 代表的是类的实例，代表当前对象的地址，而 self.class 则指向类。self 不是 python 关键字，我们把他换成 qzxdp 也是可以正常执行的:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "78e89d5a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "yumoz say: I am 10 years old.\n",
      "rain 说: 我 10 岁了，我在读 3 年级\n",
      "我叫 Tim，我是一个演说家，我演讲的主题是 Python\n"
     ]
    }
   ],
   "source": [
    "class people:\n",
    "    name = ''\n",
    "    age = 0\n",
    "    __weight = 0 # 私有属性\n",
    "    def __init__(self,n,a,w):\n",
    "        self.name = n\n",
    "        self.age = a\n",
    "        self.__weight = w\n",
    "    def speak(self):\n",
    "        print(\"%s say: I am %d years old.\" %(self.name, self.age))\n",
    "        \n",
    "#单继承示例\n",
    "class student(people):\n",
    "    grade = ''\n",
    "    def __init__(self,n,a,w,g):\n",
    "        #调用父类的构造函数\n",
    "        people.__init__(self,n,a,w)\n",
    "        self.grade = g\n",
    "    #覆写父类的方法\n",
    "    def speak(self):\n",
    "        print(\"%s 说: 我 %d 岁了，我在读 %d 年级\"%(self.name,self.age,self.grade))\n",
    "    \n",
    "#另一个类，多继承之前的准备\n",
    "class speaker():\n",
    "    topic = ''\n",
    "    name = ''\n",
    "    def __init__(self,n,t):\n",
    "        self.name = n\n",
    "        self.topic = t\n",
    "    def speak(self):\n",
    "        print(\"我叫 %s，我是一个演说家，我演讲的主题是 %s\"%(self.name,self.topic))\n",
    "# 多继承\n",
    "class sample(speaker,student):\n",
    "    a =''\n",
    "    def __init__(self,n,a,w,g,t):\n",
    "        student.__init__(self,n,a,w,g)\n",
    "        speaker.__init__(self,n,t)\n",
    "        \n",
    "p = people('yumoz',10,30)\n",
    "p.speak()\n",
    "s = student('rain',10,60,3)\n",
    "s.speak()\n",
    "# 多继承例子\n",
    "test = sample(\"Tim\",25,80,4,\"Python\")\n",
    "test.speak()   #方法名同，默认调用的是在括号中参数位置排前父类的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "8d0dce6a",
   "metadata": {},
   "source": [
    "## 方法重写"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "43493364",
   "metadata": {},
   "source": [
    "super() 函数是用于调用父类(超类)的一个方法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "94d27bc9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "调用子类方法\n",
      "调用父类方法\n"
     ]
    }
   ],
   "source": [
    "#!/usr/bin/python3\n",
    " \n",
    "class Parent:        # 定义父类\n",
    "    def myMethod(self):\n",
    "        print ('调用父类方法')\n",
    "\n",
    "class Child(Parent): # 定义子类\n",
    "    def myMethod(self):\n",
    "        print ('调用子类方法')\n",
    "\n",
    "c = Child()          # 子类实例\n",
    "c.myMethod()         # 子类调用重写方法\n",
    "super(Child,c).myMethod() #用子类对象调用父类已被覆盖的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "6adbecb2",
   "metadata": {},
   "source": [
    "# 累的属性和方法"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "1bf9e8e1",
   "metadata": {},
   "source": [
    "类的私有属性\n",
    "__private_attrs：两个下划线开头，声明该属性为私有，不能在类的外部被使用或直接访问。在类内部的方法中使用时 self.__private_attrs。\n",
    "\n",
    "类的方法\n",
    "在类的内部，使用 def 关键字来定义一个方法，与一般函数定义不同，类方法必须包含参数 self，且为第一个参数，self 代表的是类的实例。\n",
    "\n",
    "self 的名字并不是规定死的，也可以使用 this，但是最好还是按照约定使用 self。\n",
    "\n",
    "类的私有方法\n",
    "__private_method：两个下划线开头，声明该方法为私有方法，只能在类的内部调用 ，不能在类的外部调用。self.__private_methods。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "ac1b366a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n",
      "2\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'JustCounter' object has no attribute '__secretCount'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[15], line 14\u001b[0m\n\u001b[1;32m     12\u001b[0m counter\u001b[38;5;241m.\u001b[39mcount()\n\u001b[1;32m     13\u001b[0m \u001b[38;5;28mprint\u001b[39m (counter\u001b[38;5;241m.\u001b[39mpublicCount)\n\u001b[0;32m---> 14\u001b[0m \u001b[38;5;28mprint\u001b[39m (counter\u001b[38;5;241m.\u001b[39m__secretCount)\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'JustCounter' object has no attribute '__secretCount'"
     ]
    }
   ],
   "source": [
    "class JustCounter:\n",
    "    __secretCount = 0  # 私有变量\n",
    "    publicCount = 0    # 公开变量\n",
    " \n",
    "    def count(self):\n",
    "        self.__secretCount += 1\n",
    "        self.publicCount += 1\n",
    "        print (self.__secretCount)\n",
    "\n",
    "counter = JustCounter()\n",
    "counter.count()\n",
    "counter.count()\n",
    "print (counter.publicCount)\n",
    "print (counter.__secretCount)  # 报错，实例不能访问私有变量"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "cc2e8d92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "name  :  yumoz\n",
      "url :  www.gitee.cn\n",
      "这是公共方法\n",
      "这是私有方法\n"
     ]
    },
    {
     "ename": "AttributeError",
     "evalue": "'Site' object has no attribute '__foo'",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mAttributeError\u001b[0m                            Traceback (most recent call last)",
      "Cell \u001b[0;32mIn[17], line 21\u001b[0m\n\u001b[1;32m     19\u001b[0m x\u001b[38;5;241m.\u001b[39mwho()        \u001b[38;5;66;03m# 正常输出\u001b[39;00m\n\u001b[1;32m     20\u001b[0m x\u001b[38;5;241m.\u001b[39mfoo()        \u001b[38;5;66;03m# 正常输出\u001b[39;00m\n\u001b[0;32m---> 21\u001b[0m x\u001b[38;5;241m.\u001b[39m__foo()\n",
      "\u001b[0;31mAttributeError\u001b[0m: 'Site' object has no attribute '__foo'"
     ]
    }
   ],
   "source": [
    "# 类的私有方法实现\n",
    "class Site:\n",
    "    def __init__(self, name, url):\n",
    "        self.name = name       # public\n",
    "        self.__url = url   # private\n",
    " \n",
    "    def who(self):\n",
    "        print('name  : ', self.name)\n",
    "        print('url : ', self.__url)\n",
    " \n",
    "    def __foo(self):          # 私有方法\n",
    "        print('这是私有方法')\n",
    " \n",
    "    def foo(self):            # 公共方法\n",
    "        print('这是公共方法')\n",
    "        self.__foo()\n",
    "\n",
    "x = Site('yumoz', 'www.gitee.cn')\n",
    "x.who()        # 正常输出\n",
    "x.foo()        # 正常输出\n",
    "x.__foo()      # 报错"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "63a087df",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "dc68a728",
   "metadata": {},
   "source": [
    "类的专有方法：\n",
    "init : 构造函数，在生成对象时调用\n",
    "del : 析构函数，释放对象时使用\n",
    "repr : 打印，转换\n",
    "setitem : 按照索引赋值\n",
    "getitem: 按照索引获取值\n",
    "len: 获得长度\n",
    "cmp: 比较运算\n",
    "call: 函数调用\n",
    "add: 加运算\n",
    "sub: 减运算\n",
    "mul: 乘运算\n",
    "truediv: 除运算\n",
    "mod: 求余运算\n",
    "pow: 乘方"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "0feb8277",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Vector (7, 8)\n"
     ]
    }
   ],
   "source": [
    "# 运算符重载\n",
    "#!/usr/bin/python3\n",
    "\n",
    "class Vector:\n",
    "    def __init__(self, a, b):\n",
    "        self.a = a\n",
    "        self.b = b\n",
    "    def __str__(self):\n",
    "        return 'Vector (%d, %d)' % (self.a, self.b)\n",
    "    def __add__(self,other):\n",
    "        return Vector(self.a + other.a, self.b + other.b)\n",
    "\n",
    "v1 = Vector(2,10)\n",
    "v2 = Vector(5,-2)\n",
    "print (v1 + v2)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
